<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}商城{% endblock %}</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    {% block extra_css %}{% endblock %}
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="{% url 'product-list' %}">商城</a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'product-list' %}">商品</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'order-list' %}">订单</a>
                    </li>
                </ul>
                <div class="d-flex">
                    {% if user.is_authenticated %}
                        <a href="{% url 'profile' %}" class="btn btn-outline-light me-2">个人中心</a>
                        <a href="{% url 'logout' %}" class="btn btn-outline-light">登出</a>
                    {% else %}
                        <a href="{% url 'login' %}" class="btn btn-outline-light me-2">登录</a>
                        <a href="{% url 'register' %}" class="btn btn-outline-light">注册</a>
                    {% endif %}
                </div>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-3">
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        商品分类
                    </div>
                    <div class="card-body">
                        <ul class="list-group">
                            {% for category in categories %}
                            <li class="list-group-item">
                                <a href="{% url 'product_list_by_category' category.slug %}">
                                    {{ category.name }}
                                </a>
                            </li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            </div>
            <div class="col-md-9">
                <div class="card mb-4">
                    <div class="card-header bg-success text-white">
                        热门商品
                    </div>
                    <div class="card-body">
                        <div class="row">
                            {% for product in products %}
                            <div class="col-md-4 mb-4">
                                <div class="card h-100">
                                    <img src="{{ product.image.url }}" class="card-img-top" alt="{{ product.name }}">
                                    <div class="card-body">
                                        <h5 class="card-title">{{ product.name }}</h5>
                                        <p class="card-text">¥{{ product.price }}</p>
                                        <a href="{% url 'product_detail' product.id product.slug %}" class="btn btn-primary">查看详情</a>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
                {% if user.is_authenticated %}
                <div class="card">
                    <div class="card-header bg-info text-white">
                        我的订单
                    </div>
                    <div class="card-body">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>订单号</th>
                                    <th>日期</th>
                                    <th>总价</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for order in orders %}
                                <tr>
                                    <td>{{ order.id }}</td>
                                    <td>{{ order.created }}</td>
                                    <td>¥{{ order.get_total_cost }}</td>
                                    <td>{{ order.get_status_display }}</td>
                                    <td>
                                        <a href="{% url 'order_detail' order.id %}" class="btn btn-sm btn-outline-primary">详情</a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>
        {% block content %}{% endblock %}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    {% block extra_js %}{% endblock %}
</body>
</html>